package com.atguigu.gmall.gateway.service;


import com.atguigu.gmall.common.constant.RedisConstant;
import com.atguigu.gmall.model.user.UserInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserAuthService {


    @Autowired
    StringRedisTemplate redisTemplate;

    ObjectMapper objectMapper = new ObjectMapper();

    public Long checkToken(String token) {
        String json = redisTemplate.opsForValue().get(RedisConstant.LOGIN_USER + token);
        if(StringUtils.isEmpty(json)){
            //无此用户
            return null;
        }else {
            try {
                //把json转成userinfo提取到id
                UserInfo userInfo = objectMapper.readValue(json, UserInfo.class);
                return userInfo.getId();
            }catch (Exception e){
                return null;
            }
        }

    }
}
